package com.tnl.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;
import com.tnl.app.dao.*;
import com.tnl.app.entity.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.biz.hunter.db.entity.UserInfo;
import com.tnl.app.service.IEmployeeService;
import com.tnl.app.service.IPersonService;
import com.tnl.app.service.IRecommendService;
import com.tnl.app.service.UserService;
import com.tnl.app.service.impl.SMSService;
import com.tnl.app.vo.PageVo;
import com.tnl.base.web.JsonController;
import com.tnl.utils.DateUtil;

@Controller
@RequestMapping("/company")
public class CompanyController extends JsonController {
	@Autowired
	private UserService userService;
	@Autowired
	private AdcarouselDAO adcarouselDAO;
	@Autowired
	private AdChannelDAO adChannelDAO;
	@Autowired
	private ArticleDAO articleDAO;
	@Autowired
	private JobDAO jobDAO;
	@Autowired
	private CityDAO cityDAO;
	@Autowired
	private CompanyDAO companyDAO;
	@Autowired
	private RecommendDAO recommendDAO;
	@Autowired
	private JobTitleDAO jobTitleDAO;	
	@Autowired
	private WxAppUserDAO wxAppUserDAO;
	@Autowired
	private DomainDAO domainDAO;
	@Autowired
	private HunterDAO hunterDAO;
	@Autowired
	private IPersonService personService;
	@Autowired
	private IRecommendService recommendService;
	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private SMSService smsService;
	@Autowired
	private CompanyTrendDAO companyTrendDAO;

	@Autowired
	DictionaryDAO dictionaryDAO;
	/**
	 * 主页展示公司信息-公司信息
	 * @param page
	 * @param size
	 * @param response
	 */
	@RequestMapping(value = "getCompanyList")
	public void getCompanyList(Integer page,Integer size,HttpServletResponse response) {
		//Date todayDate = new Date();
		//long today = todayDate.getTime();
		int totalNum=companyDAO.getTotalNumForCompany();
		int pageNum=getPageNum(totalNum,size);
		int offset=(page-1)*size;
		List<Company> companylist = companyDAO.companyList(offset,size);
		for (Company company : companylist) {
			int cityId=company.getCityId();
			City city=cityDAO.getById(cityId);
			company.setCityName(city.getName());
			Domain domain=domainDAO.getById(company.getDomain());
			company.setDomainName(domain.getName());
			
			long num = jobDAO.getSumByCompanyId(company.getId());
			company.setJobNum(num);
		}
		this.successJson(new PageVo(companylist,pageNum), "公司", response);
	}
	
	@RequestMapping(value = "getIndexCompanies")
	public void getIndexCompanies(Integer page,Integer size,
			@RequestParam(required = false) String companyName,
			@RequestParam(defaultValue = "1") String tags,HttpServletResponse response) {

		int totalNum=companyDAO.getTotalNumCompany(companyName,tags);
		int pageNum=getPageNum(totalNum,size);
		int offset=(page-1)*size;
		List<Company> companylist = companyDAO.indexCompanies(offset,size,companyName,tags);
		for (Company company : companylist) {
			int cityId=company.getCityId();
			City city=cityDAO.getById(cityId);
			company.setCityName(city.getName());
			Domain domain=domainDAO.getById(company.getDomain());
			company.setDomainName(domain.getName());
			
			long num = jobDAO.getSumByCompanyId(company.getId());
			company.setJobNum(num);
			
		}
		this.successJson(new PageVo(companylist,pageNum), "公司", response);
	}
	
	/**
	 * 查询公司详情
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "getCompanyDetail")
	public void getCompanyDetail(int id,HttpServletResponse response){
		Company company=companyDAO.getById(id);
		Domain domain=domainDAO.getById(company.getDomain());
		company.setDomainName(domain.getName());
		List<CompanyTrend> trends = companyTrendDAO.getListByCompanyId(id);
		company.setCompanyTrends(trends);
		this.successJson(company, "公司详细信息", response);
	}

	/**
	 * 根据公司查询职位
	 * @param companyId
	 * @param response
	 */
	@RequestMapping(value="getJobdescByCompany")
	public void getJobdescByCompany(int companyId,HttpServletResponse response){
		List<JobDesc> job=companyDAO.getJobdescByCompany(companyId);
		for (JobDesc jobDesc : job) {
			// get city
			int cityId = jobDesc.getCityId();
			City city = cityDAO.getById(cityId);
			if (city != null) {
				jobDesc.setCityName(city.getName());
			}
			// get company
			Company company = companyDAO.getById(jobDesc.getCompanyId());
			jobDesc.setCompanyName(company.getName());
			// get expiredate
			long data = jobDesc.getExpiredDate();
			jobDesc.setExpiredDateStr(DateUtil.getDateStr(data));
			Domain domain = domainDAO.getById(jobDesc.getDomainID());
			if (domain != null) {
				jobDesc.setDomainName(domain.getName());
			}

			jobDesc.setCreateTime(DateUtil.getDateStr(jobDesc.getCreateDate()));
			
			//列表不需要信息，影响前台渲染速度
			jobDesc.setDescription("");
			jobDesc.setMajorDuty("");
		}
		job.stream().parallel().filter(s -> !StringUtils.isEmptyOrWhitespaceOnly(s.getKeywords())).peek(s -> {
			setKeyWords(s);
		}).collect(Collectors.toList());
		this.successJson(job, "公司职位", response);
	}

	private void setKeyWords(JobDesc s) {
		String keywords = s.getKeywords();
		String[] tagList = keywords.split(",");
		List<String> tags = Arrays.asList(tagList);
		List<String> keyList = tags.stream().filter(p->!Objects.equals("0", p)).collect(Collectors.toList());
		if(keyList == null || keyList.size() <= 0){
			return;
		}
		List<Dictionary> dictionary = dictionaryDAO.listByTypeAndValue("position_slogan", keyList);
		List<String> tag = dictionary.stream().map(Dictionary::getLabel).collect(Collectors.toList());
		s.setTagList(tag);
	}

	/**
	 * 查询公司视频,文章
	 * @param companyId
	 * @param response
	 */
	@RequestMapping(value="getCompanyByVideo")
	public void getCompanyByVideo(int companyId,int media_type,HttpServletResponse response){
		List<Adcarousel> ad=adcarouselDAO.getCompanyByVideo(companyId,media_type);
		this.successJson(ad, "company-video", response);
	}
	
	/**
	 * 查职能,公司
	 * @param searchName
	 * @param pid
	 * @param cid
	 * @param response
	 */
	@RequestMapping(value="getSearchResult")
	public void getSearchResult(String searchName,int pid,int cid,HttpServletResponse response){
		if(pid!=0){
			List<JobTitle> result=jobTitleDAO.matchByName(searchName);
			this.successJson(result, "职能", response);
		}else if(cid!=0){
			List<Company> res=companyDAO.matchByName(searchName);
			this.successJson(res, "公司", response);
		}
	}
	/**
	 * index searchName
	 * @param response
	 * @param page
	 * @param size
	 * @param searchName
	 */
	@RequestMapping(value="searchCompany")
	public void searchCompany(HttpServletResponse response,Integer page,Integer size,String searchName){
		System.out.println("searchCompany print searchName");
		int totalNum=companyDAO.getTotalNumForSearchCompany(searchName);
		int pageNum=getPageNum(totalNum,size);
		int offset=(page-1)*size;
		List<Company> companylist = companyDAO.searchCompany(offset, size, searchName);
		for (Company company : companylist) {
			int cityId=company.getCityId();
			if(cityId!=0) {
				City city=cityDAO.getById(cityId);
				company.setCityName(city.getName());
			}
			int domainId=company.getDomain();
			if(domainId!=0) {
				Domain domain=domainDAO.getById(domainId);
				company.setDomainName(domain.getName());
			}
		}
		this.successJson(new PageVo(companylist,pageNum), "公司", response);
	}
	
	

}
